var server = require('../../utils/server');
var common = require('../../utils/common');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketId: "",
    item: "",
    poptype: 3,
    couponScoreNum: "",
    title: "",
    isCanExchange: true,
    amount: "",
    isCanAdd:true,
    curGoodsId:'',
    couponparams:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      ticketId: options.ticketid
    })
    console.log(this.data.ticketId)
  },
  onShow: function() {
    this.getVoucherDetail();
    console.log(this.data.poptype)
  },
  getVoucherDetail() {
    server.postJSON(common.storePath + 'coupon/scoremalldetail/', {
      "score_mall_id": this.data.ticketId
    }, res => {
      this.setData({
        item: res.data.data
      })

    })
  },
  showConfirmCont() {
    server.postJSON(common.storePath + 'coupon/isexchange/', {
      "score_mall_id":this.data.ticketId
    }, res => {
      if (res.data.errno == '0') {
        this.setData({
          poptype: 0,
          title: this.data.item.name,
          couponScoreNum: this.data.item.score,
          amount: this.data.item.amount,
          retailprice: this.data.item.retail_price,
          oldprice: this.data.item.old_price,
          curGoodsId: this.data.item.goods_id,
          couponparams: this.data.item.coupon_params
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  exchangeGoods() {
    this.setData({
      isCanExchange: false
    })
    var param = {
      "score_mall_id": this.data.ticketId
    }
    this.confirmExchangeTicket(param);
  },
  getcoupon(params){
    console.log(222)
    this.setData({
      isCanExchange: false
    })
    var param = {
      "score_mall_id": this.data.ticketId,
      "send_coupon_result": params.detail.send_coupon_result
    }
    this.confirmExchangeTicket(param);
  },
  confirmExchangeTicket(param){
    server.postJSON(common.storePath + 'coupon/scoremallexchange/', param, res => {
      if (res.data.errno == '0') {
        if(param.send_coupon_result!=undefined||param.send_coupon_result!=''){
          this.getVoucherDetail()
        }
        this.setData({
          poptype: 2,
          isCanExchange: true
        })
        wx.getStorage({
          key: 'userInfo',
          success: resp => {
            resp.data.coupon_num = parseInt(resp.data.coupon_num) + 1;
            wx.setStorage({
              key: "userInfo",
              data: resp.data
            })
          },
        })

      } else {
        common.showErrorToast(res.data.errmsg);

        this.setData({
          isCanExchange: true
        })
      }
    })
  },
  hideCouponPop() {
    this.setData({
      poptype: 3
    })
  },
  useCoupon() {
    this.setData({
      isCanAdd: false
    })
    server.postJSON(common.storePath + 'cart/addgoods/', {
      "store_id": wx.getStorageSync('storeId'),
      "goods_id": this.data.item.goods_id,
      "trace_id": '',
      "scene": ''
    }, res => {
      if (res.data.errno == "0") {
        common.showErrorToast("添加成功");
        this.setData({
          isCanAdd: true,
          poptype: 3
        })
      } else {
        this.setData({
          isCanAdd: true
        })
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  goToProductsIndex(){
    app.globalData.skipType = 2;
    this.setData({
      poptype: 3
    })
    wx.navigateTo({
      url: '../shop/productsListIndex',
    })
    
  }
})